// pages/Carrier/Carrier.js
// pages/commit/index.js
// import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active:0,
    activeStep:0,
    campTypess: ['综合营地', '微型营地', '帐篷营地', '教育营地', '运动营地'],
    campTypeIndex: 0,
    region: ['河北省', '石家庄市', '灵寿县'],
    openTimes: ['全年', '月份'],
    openTimeIndex: 0,
    images: [],
    isPublicPhone: false,
    steps: [
      {
        text: '步骤一',
        desc: '营地信息',
      },
      {
        text: '步骤二',
        desc: '服务设施',
      },
      {
        text: '步骤三',
        desc: '营地介绍',
      },
      {
        text: '步骤四',
        desc: '预览',
      },
    ],
    campTypes: [
      { name: '休闲度假', value: 'mountain', icon: '/images/onYesTap_1/休闲度假.png', checked: false },
      { name: '教育研学', value: 'beach', icon: '/images/onYesTap_1/教育研学.png', checked: false },
      { name: '文体赛站', value: 'wenti', icon: '/images/onYesTap_1/文体赛站.png', checked: false },
      { name: '民宿农庄', value: 'minsu', icon: '/images/onYesTap_1/民宿农庄.png', checked: false },
      { name: '服务配套', value: 'fuwuqu', icon: '/images/onYesTap_1/服务区配套.png', checked: false },
      { name: '景区配套', value: 'jingqu', icon: '/images/onYesTap_1/景区配套.png', checked: false },
      { name: '综合', value: 'zonghe', icon: '/images/onYesTap_1/综合.png', checked: false },
      { name: '其他', value: 'qita', icon: '/images/onYesTap_1/其他.png', checked: false },
      // 更多选项...
    ],
    serviceFacilities: [
      { name: '房车营位', value: 'hot_shower', icon: '/images/onYesTap_1/房车营位.png', checked: false },
      { name: '帐篷出租', value: 'zhangpeng', icon: '/images/onYesTap_1/帐篷出租.png', checked: false },
      { name: 'WIFI', value: 'wifi', icon: '/images/onYesTap_1/WIFI.png', checked: false },
      { name: '自来水', value: 'zilaishui', icon: '/images/onYesTap_1/自来水.png', checked: false },
      { name: '营位供电', value: 'gongdian', icon: '/images/onYesTap_1/营位供电.png', checked: false },
      { name: '卫生间', value: 'wc', icon: '/images/onYesTap_1/卫生间.png', checked: false },
      { name: '淋浴间', value: 'linyu', icon: '/images/onYesTap_1/淋浴间.png', checked: false },
      { name: '停车场', value: 'tingche', icon: '/images/onYesTap_1/停车场.png', checked: false },
      { name: '商店', value: 'shangdian', icon: '/images/onYesTap_1/商店.png', checked: false },
      { name: '热饮用水', value: 'reyin', icon: '/images/onYesTap_1/热饮用水.png', checked: false },
      { name: '洗衣房', value: 'xiyi', icon: '/images/onYesTap_1/洗衣房.png', checked: false },
      { name: '烧烤设备', value: 'shaokao', icon: '/images/onYesTap_1/烧烤设备.png', checked: false },
      { name: '酒吧茶室', value: 'chahsi', icon: '/images/onYesTap_1/酒吧茶室.png', checked: false },
      { name: '卡拉ok', value: 'kalaok', icon: '/images/onYesTap_1/卡拉ok.png', checked: false },
      { name: '露天影院', value: 'yingyuan', icon: '/images/onYesTap_1/露天影院.png', checked: false },
      { name: '会议室', value: 'huiyishi', icon: '/images/onYesTap_1/会议室.png', checked: false },
      { name: '游乐设施', value: 'youle', icon: '/images/onYesTap_1/游乐设施.png', checked: false },
      { name: '洗车设备', value: 'xiche', icon: '/images/onYesTap_1/洗车设备.png', checked: false },
      { name: '房车维修', value: 'weixiu', icon: '/images/onYesTap_1/房车维修.png', checked: false },
      { name: '其他', value: 'qita1', icon: '/images/onYesTap_1/其他.png', checked: false },
      // 更多选项...
    ],
    accommodations: [
      { name: '房车', value: 'tent', icon: '/images/onYesTap_1/房车.png', checked: false },
      { name: '帐篷房', value: 'zhangpengfang', icon: '/images/onYesTap_1/帐篷房.png', checked: false },
      { name: '木屋', value: 'muwu', icon: '/images/onYesTap_1/木屋.png', checked: false },
      { name: '民宿', value: 'minsu', icon: '/images/onYesTap_1/民宿.png', checked: false },
      { name: '酒店', value: 'jiudian', icon: '/images/onYesTap_1/酒店.png', checked: false },
      { name: '集装箱', value: 'jizhuangxiang', icon: '/images/onYesTap_1/集装箱.png', checked: false },
      { name: '其他', value: 'qita2', icon: '/images/onYesTap_1/其他.png', checked: false },
      // 更多选项...
    ],
    campingConditions: [
      { name: '可停营位旁', value: 'picnic_area', icon: '/images/onYesTap_1/可停营位旁.png', checked: false },
      { name: '可过夜', value: 'huoye', icon: '/images/onYesTap_1/可过夜.png', checked: false },
      { name: '可烧烤', value: 'shaokao', icon: '/images/onYesTap_1/可烧烤.png', checked: false },
      { name: '可携带宠物', value: 'chongwu', icon: '/images/onYesTap_1/可携带宠物.png', checked: false },
      { name: '可篝火', value: 'gouhuo', icon: '/images/onYesTap_1/可篝火.png', checked: false },
      { name: '其他', value: 'qita3', icon: '/images/onYesTap_1/其他.png', checked: false },
      // 更多选项...
    ],
    campEnvironments: [
      { name: '城市', value: 'forest', icon: '/images/onYesTap_1/城市.png', checked: false },
      { name: '城郊', value: 'chengjiao', icon: '/images/onYesTap_1/城郊.png', checked: false },
      { name: '平原', value: 'pingyuan', icon: '/images/onYesTap_1/平原.png', checked: false },
      { name: '草原', value: 'caoyuan', icon: '/images/onYesTap_1/草原.png', checked: false },
      { name: '山地', value: 'shandi', icon: '/images/onYesTap_1/山地.png', checked: false },
      { name: '林地', value: 'lindi', icon: '/images/onYesTap_1/林地.png', checked: false },
      { name: '海滨', value: 'haibin', icon: '/images/onYesTap_1/海滨.png', checked: false },
      { name: '亲水', value: 'qinshui', icon: '/images/onYesTap_1/亲水.png', checked: false },
      { name: '沙漠', value: 'shamo', icon: '/images/onYesTap_1/沙漠.png', checked: false },
      { name: '其他', value: 'qita4', icon: '/images/onYesTap_1/其他.png', checked: false },
      // 更多选项...
    ],
    introduction: '',
    isAcceptingVehicles: false,
    vehicleType: null
  },

  /** 3 */
  onInputIntroduction(e) {
    this.setData({
      introduction: e.detail.value
    });
  },
  onSwitchChange(e) {
    this.setData({
      isAcceptingVehicles: e.detail.value
    });
  },
  onAddTypeClick() {
    // 跳转到添加车辆类型的页面
    wx.navigateTo({
      url: '/pages/addVehicleType/addVehicleType'
    })
  },
  onLoad(query) {
    // 监听子页面传递的数据
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenedPage', (data) => {
      console.log(data); // 打印接收到的数据
      // 更新页面显示
      this.setData({
        vehicleType: data
      });
    });
  },

  bindCampTypeChange: function(e) {
    this.setData({
      campTypeIndex: e.detail.value
    });
  },
  bindRegionChange: function(e) {
    this.setData({
      region: e.detail.value
    });
  },

  bindOpenTimeChange: function(e) {
    this.setData({
      openTimeIndex: e.detail.value
    });
  },

  chooseImage: function() {
    const that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          images: that.data.images.concat(res.tempFilePaths)
        });
      }
    });
  },
  bindSwitchChange: function(e) {
    this.setData({
      isPublicPhone: e.detail.value
    });
  },
  formSubmit: function(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    // 这里可以添加提交数据到服务器的逻辑
  },
  saveDraft: function() {
    const formData = this.data;
    console.log('保存草稿，当前数据为：', formData);
    // 这里可以添加保存草稿的逻辑
  },
  nextStep(){
    let newActiveStep = this.data.active + 1;
    if (newActiveStep <= 3) {
      this.setData({
        active: newActiveStep
      });
    } else {
      wx.showToast({
        title: '所有步骤已完成',
        icon: 'success'
      });
    }
  },
  prevStep() {
    let newActiveStep = this.data.active - 1;
    if (newActiveStep >= 0) {
      this.setData({
        active: newActiveStep
      });
    } else {
      wx.showToast({
        title: '已经是第一步',
        icon: 'none'
      });
    }
  },
  onOptionTap(e) {
    const index = e.currentTarget.dataset.index;
    const category = e.currentTarget.dataset.category;

    let newData = {};
    switch (category) {
      case 'campTypes':
        newData.campTypes = this.data.campTypes.map((item, i) => {
          if (i === index) {
            return { ...item, checked: !item.checked };
          }
          return item;
        });
        break;
      case 'serviceFacilities':
        newData.serviceFacilities = this.data.serviceFacilities.map((item, i) => {
          if (i === index) {
            return { ...item, checked: !item.checked };
          }
          return item;
        });
        break;
      case 'accommodations':
        newData.accommodations = this.data.accommodations.map((item, i) => {
          if (i === index) {
            return { ...item, checked: !item.checked };
          }
          return item;
        });
        break;
      case 'campingConditions':
        newData.campingConditions = this.data.campingConditions.map((item, i) => {
          if (i === index) {
            return { ...item, checked: !item.checked };
          }
          return item;
        });
        break;
      case 'campEnvironments':
        newData.campEnvironments = this.data.campEnvironments.map((item, i) => {
          if (i === index) {
            return { ...item, checked: !item.checked };
          }
          return item;
        });
        break;
    }

    this.setData(newData);
  },

  onChangeCampType(e) {
    const values = e.detail.value;
    this.setData({
      campTypes: this.data.campTypes.map(item => ({
        ...item,
        checked: values.includes(item.value)
      }))
    });
  },

  onChangeServiceFacilities(e) {
    const values = e.detail.value;
    this.setData({
      serviceFacilities: this.data.serviceFacilities.map(item => ({
        ...item,
        checked: values.includes(item.value)
      }))
    });
  },

  onChangeAccommodation(e) {
    const values = e.detail.value;
    this.setData({
      accommodations: this.data.accommodations.map(item => ({
        ...item,
        checked: values.includes(item.value)
      }))
    });
  },

  onChangeCampingConditions(e) {
    const values = e.detail.value;
    this.setData({
      campingConditions: this.data.campingConditions.map(item => ({
        ...item,
        checked: values.includes(item.value)
      }))
    });
  },

  onChangeCampEnvironment(e) {
    const values = e.detail.value;
    this.setData({
      campEnvironments: this.data.campEnvironments.map(item => ({
        ...item,
        checked: values.includes(item.value)
      }))
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(query) {
    // 监听子页面传递的数据
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('acceptDataFromOpenedPage', (data) => {
      console.log(data); // 打印接收到的数据
      // 更新页面显示
      this.setData({
        vehicleType: data
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})